package com.pshcp.tool;

import com.external.hospital.im.domain.ConnectParamsDTO;
import com.external.hospital.im.domain.FinishConsultSessionDTO;
import com.external.hospital.im.domain.InitConsultSessionBO;
import com.external.hospital.im.domain.InitConsultSessionDTO;
import com.external.hospital.im.domain.SendMessageDTO;
import com.external.hospital.im.domain.SendMessageDataRes;
import com.external.hospital.im.domain.SendMessageListDTO;
import com.external.hospital.im.domain.SendMessageRes;
import com.framework.base.BaseDTO;
import com.framework.exception.BizException;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.ao.im.SendMessageAO;
import com.pshcp.ao.im.SendMessagePageAO;
import com.pshcp.domain.model.OnlineSession;
import com.external.hospital.im.domain.SendMessageBO;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/12/10 14:49
 */
public class ImTool {

    public static ConnectParamsDTO buildConnectParamsDTO(OnlineSession onlineSession) {
        ConnectParamsDTO connectParamsDTO = new ConnectParamsDTO();
        connectParamsDTO.setToken(ReqComTool.getToken());
        /**
         * 类型：0 app-医生 1 app-患者 2 微信患者
         */
        Integer terminal = ReqComTool.getTerminal();
        connectParamsDTO.setUserId(terminal == 0 ? ReqComTool.getUserId() : onlineSession.getPatientId());
        /**
         * 应用来源 0：患者App,1：医生App,2：患者微信,3：互联网医院(PC)
         */
        Integer origin = terminal == 1 ? 0 : (terminal == 2 ? 2 : (terminal == 0 ? 1 : null));
        connectParamsDTO.setOrigin(origin);
        return connectParamsDTO;
    }

    public static InitConsultSessionDTO buildInitConsultSession(OnlineSession onlineSession) {
        InitConsultSessionDTO initConsultSessionDTO = new InitConsultSessionDTO();
        initConsultSessionDTO.setDoctorId(onlineSession.getDoctorId());
        initConsultSessionDTO.setDoctorName(onlineSession.getDoctorName());
        initConsultSessionDTO.setPatientId(onlineSession.getPatientId());
        initConsultSessionDTO.setPatientName(onlineSession.getPatientName());
        return initConsultSessionDTO;
    }

    public static OnlineSession buildInit2UpdateOnlineSession(OnlineSession onlineSession, InitConsultSessionBO initConsultSessionBO) {
        onlineSession.setSessionId(initConsultSessionBO.getSessionId());
        onlineSession.setBeginTime(initConsultSessionBO.getBeginTime());
        return onlineSession;
    }

    public static FinishConsultSessionDTO buildFinishConsultSession(OnlineSession onlineSession) {
        FinishConsultSessionDTO finishConsultSessionDTO = new FinishConsultSessionDTO();
        finishConsultSessionDTO.setDoctorId(onlineSession.getDoctorId());
        finishConsultSessionDTO.setPatientId(onlineSession.getPatientId());
        finishConsultSessionDTO.setTriggerSource(1);
        return finishConsultSessionDTO;
    }

    public static OnlineSession buildFinish2UpdateOnlineSession(OnlineSession onlineSession) {
        onlineSession.setEndTime(System.currentTimeMillis());
        onlineSession.setSessionStatus(2);
        return onlineSession;
    }

    public static void checkSendMessage(SendMessageAO sendMessageAO, MultipartFile multipartFile) throws BizException {
        CommonTool.checkString(sendMessageAO.getSessionId(), "10200008");
        CommonTool.checkObj(sendMessageAO.getType(), "10200003");
        CommonTool.checkObj(sendMessageAO.getSendTime(), "10200004");
        if (sendMessageAO.getType() != 1 && sendMessageAO.getType() != 2 && sendMessageAO.getType() != 4) {
            CommonTool.throwBizException("10200005");
        }
        if (sendMessageAO.getType() == 1) {
            CommonTool.checkString(sendMessageAO.getContent(), "10200007");
        }
        if (sendMessageAO.getType() == 2 || sendMessageAO.getType() == 4) {
            CommonTool.checkObj(multipartFile, "10200007");
        }
    }

    public static SendMessageDTO buildSendMessageDTO(OnlineSession onlineSession, SendMessageAO sendMessageAO, MultipartFile multipartFile) {
        SendMessageDTO sendMessageDTO = new SendMessageDTO();
        sendMessageDTO.setSessionId(onlineSession.getSessionId());
        Integer terminal = ReqComTool.getTerminal();
        Integer origin = terminal == 1 ? 0 : (terminal == 2 ? 2 : (terminal == 0 ? 1 : null));
        sendMessageDTO.setOrigin(origin);
        sendMessageDTO.setSendTime(sendMessageAO.getSendTime());
        Integer relation = terminal == 0 ? 1 : 0;
        /**
         * 0 患者给医生
         * 1 医生给患者
         */
        sendMessageDTO.setRelation(relation);
        sendMessageDTO.setFromId(relation == 0 ? onlineSession.getPatientId() : onlineSession.getDoctorId());
        sendMessageDTO.setToId(relation == 0 ? onlineSession.getDoctorId() : onlineSession.getPatientId());
        sendMessageDTO.setType(sendMessageAO.getType());
        if (sendMessageAO.getType() == 1) {
            sendMessageDTO.setContent(sendMessageAO.getContent());
        }
        if (sendMessageAO.getType() == 2 || sendMessageAO.getType() == 4) {
            sendMessageDTO.setToken(ReqComTool.getToken());
            sendMessageDTO.setFilename(multipartFile.getOriginalFilename());
            File file = transferToFile(multipartFile);
            sendMessageDTO.setFile(file);
        }
        return sendMessageDTO;
    }


    private static File transferToFile(MultipartFile multipartFile) {
        File file = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            String prefix = originalFilename.substring(originalFilename.lastIndexOf("."));
            file = File.createTempFile(originalFilename, prefix);
            multipartFile.transferTo(file);
            //删除
            file.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static SendMessageBO buildSendMessageVO(SendMessageAO sendMessageAO, SendMessageDataRes sendMessageDataRes) {
        SendMessageRes sendMessageRes = sendMessageDataRes.getData();
        SendMessageBO sendMessageVO = new SendMessageBO();
        sendMessageVO.setType(sendMessageAO.getType());
        sendMessageVO.setId(sendMessageRes.getId());
        sendMessageVO.setSendTime(sendMessageRes.getSendTime());
        return sendMessageVO;
    }

    public static OnlineSession buildSendMessage2UpdateOnlineSession(OnlineSession onlineSession, SendMessageBO sendMessageBO) {
        onlineSession.setLastMsgType(sendMessageBO.getType());
        onlineSession.setLastMsgContent(sendMessageBO.getContent());
        return onlineSession;
    }

    public static void checkGetSendMessagePage(SendMessagePageAO sendMessagePageAO) throws BizException {
        CommonTool.checkString(sendMessagePageAO.getSessionId(), "10200007");
    }


    public static SendMessageListDTO buildGetSendMessagePage(SendMessagePageAO sendMessagePageAO,OnlineSession onlineSession) {
        BaseDTO baseDTO = CommonTool.buildBaseDTO(sendMessagePageAO.getPageNum(), sendMessagePageAO.getPageSize());
        SendMessageListDTO sendMessageListDTO = new SendMessageListDTO();
        sendMessageListDTO.setPageNo(baseDTO.getPageNum());
        sendMessageListDTO.setPageSize(baseDTO.getPageSize());
        Integer terminal = ReqComTool.getTerminal();
        Integer origin = terminal == 1 ? 0 : (terminal == 2 ? 2 : (terminal == 0 ? 1 : null));
        sendMessageListDTO.setOrigin(origin);
        sendMessageListDTO.setDoctorId(onlineSession.getDoctorId());
        sendMessageListDTO.setPatientId(onlineSession.getPatientId());
        sendMessageListDTO.setSessionId(onlineSession.getSessionId());
        sendMessageListDTO.setEndTime(sendMessagePageAO.getEndTime());
        return sendMessageListDTO;
    }

}
